package com.tim.app.ui.fragment.home;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.base.BaseFragment;
import com.application.library.net.StringResponseCallback;
import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.server.result.UserinfoDetailResult;
import com.tim.app.ui.activity.MainActivity;
import com.tim.app.ui.activity.UserFriendActivity;
import com.tim.app.ui.activity.person_center.AboutActivity;
import com.tim.app.ui.activity.person_center.HelpCenterActivity;
import com.tim.app.ui.activity.person_center.InvateRedbagActivity;
import com.tim.app.ui.activity.person_center.MyBrowseHistoryActivity;
import com.tim.app.ui.activity.person_center.MyCollectionActivity;
import com.tim.app.ui.activity.person_center.MyFollowActivity;
import com.tim.app.ui.activity.person_center.MyIncomeActivity;
import com.tim.app.ui.activity.person_center.SomeOneDynamicActivity;
import com.tim.app.ui.activity.setting.SettingActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneAnswerActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneArticleActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneQuestionActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneStoryActivity;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;

/**
 * 我的页面
 */
public class HomeMeFragment extends BaseFragment implements View.OnClickListener {

    public static final String TAG = "HomeMeFragment";
    private View rootView;

    private TextView tvUserName;
    private TextView tvSignature;

    private LinearLayout llAvatarContainer;
    private RoundedImageView rivAvatar;
    private RelativeLayout rlHeader;

    private LinearLayout llCountInfo;
    private TextView tvStoryCount;//故事数量
    private TextView tvAnswerCount;//回答数量
    private TextView tvDynmaicCount;//动态数量
    private TextView tvQuestionCount;//问题数量
    private TextView tvArticleCount;//文章数量

    private LinearLayout llStory;
    private LinearLayout llAnswer;
    private LinearLayout llDynamic;
    private LinearLayout llQuestion;
    private LinearLayout llArticle;

    private RelativeLayout rlMyFriend;//我的好友
    private RelativeLayout rlDynamic;//我的动态
    private RelativeLayout rlMyFollow;//我的关注
    private RelativeLayout rlMyCollect;//我的收藏
    private RelativeLayout rlBrowserHistory;//浏览记录

    private RelativeLayout rlHelpCenter;//帮助中心
    private RelativeLayout rlAboutUs;//关于我们
    private RelativeLayout rlSetting;//设置
    private RelativeLayout rl_invite_redbag;//邀请红包
    private RelativeLayout rl_my_income;//我的收入

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_tab_me, container, false);

            rlHeader = (RelativeLayout) rootView.findViewById(R.id.rlHeader);

            tvUserName = (TextView) rootView.findViewById(R.id.tvUserName);

            tvSignature = (TextView) rootView.findViewById(R.id.tvSignature);

            llAvatarContainer = (LinearLayout) rootView.findViewById(R.id.llAvatarContainer);
            rivAvatar = (RoundedImageView) rootView.findViewById(R.id.rivAvatar);

            llCountInfo = (LinearLayout) rootView.findViewById(R.id.llCountInfo);
            tvStoryCount = (TextView) rootView.findViewById(R.id.tvStoryCount);
            tvAnswerCount = (TextView) rootView.findViewById(R.id.tvAnswerCount);
            tvDynmaicCount = (TextView) rootView.findViewById(R.id.tvDynmaicCount);
            tvQuestionCount = (TextView) rootView.findViewById(R.id.tvQuestionCount);
            tvArticleCount = (TextView) rootView.findViewById(R.id.tvArticleCount);

            llStory = (LinearLayout) rootView.findViewById(R.id.llStory);
            llAnswer = (LinearLayout) rootView.findViewById(R.id.llAnswer);
            llDynamic = (LinearLayout) rootView.findViewById(R.id.llDynamic);
            llQuestion = (LinearLayout) rootView.findViewById(R.id.llQuestion);
            llArticle = (LinearLayout) rootView.findViewById(R.id.llArticle);

            rlMyFriend = (RelativeLayout) rootView.findViewById(R.id.rlMyFriend);
            rlDynamic = (RelativeLayout) rootView.findViewById(R.id.rlDynamic);
            rlMyFollow = (RelativeLayout) rootView.findViewById(R.id.rlMyFollow);
            rlMyCollect = (RelativeLayout) rootView.findViewById(R.id.rlMyCollect);
            rlBrowserHistory = (RelativeLayout) rootView.findViewById(R.id.rlBrowserHistory);
            rlHelpCenter = (RelativeLayout) rootView.findViewById(R.id.rlHelpCenter);
            rlAboutUs = (RelativeLayout) rootView.findViewById(R.id.rlAboutUs);
            rlSetting = (RelativeLayout) rootView.findViewById(R.id.rlSetting);
            rl_invite_redbag= (RelativeLayout) rootView.findViewById(R.id.rl_invite_redbag);
            rl_my_income= (RelativeLayout) rootView.findViewById(R.id.rl_my_income);

            rl_my_income.setOnClickListener(this);
            rl_invite_redbag.setOnClickListener(this);
            llAvatarContainer.setOnClickListener(this);
            rlHeader.setOnClickListener(this);
            llCountInfo.setOnClickListener(this);
            rlMyFriend.setOnClickListener(this);
            rlDynamic.setOnClickListener(this);
            rlMyFollow.setOnClickListener(this);
            rlMyCollect.setOnClickListener(this);
            rlBrowserHistory.setOnClickListener(this);
            rlSetting.setOnClickListener(this);
            tvSignature.setOnClickListener(this);
            llStory.setOnClickListener(this);
            llAnswer.setOnClickListener(this);
            llDynamic.setOnClickListener(this);
            llQuestion.setOnClickListener(this);
            llArticle.setOnClickListener(this);
            rlHelpCenter.setOnClickListener(this);
            rlAboutUs.setOnClickListener(this);
        }
        UserManager.ins().loadUserInfo(new UserManager.LoadCallBack() {
            @Override
            public void onComlpete() {
                if (UserManager.ins().isLogin()) {
                    refreshUi(UserManager.ins().getLoginUser());
                    initData();
                }
            }
        });
        EventManager.ins().registListener(EventTag.ACCOUNT_UPDATE_INFO, eventListener);
        return rootView;
    }

    private void refreshUi(User user) {
        if (null == user) {
            return;
        }
        if (!TextUtils.isEmpty(user.getImage())) {
            BitmapLoader.ins().loadImage(user.getImage(), rivAvatar);
        }
        if (!TextUtils.isEmpty(user.getName())) {
            tvUserName.setText(user.getName());
        }
        tvStoryCount.setText(String.valueOf(user.getStoryCount()));
        tvAnswerCount.setText(String.valueOf(user.getAnswerCount()));
        tvDynmaicCount.setText(String.valueOf(user.getSpeakCount()));
        tvArticleCount.setText(String.valueOf(user.getPublishCount()));
        tvQuestionCount.setText(String.valueOf(user.getProblemCount()));
    }

    private void initData() {
        API_User.ins().getUserInfoById(TAG, UserManager.ins().getUserId(), new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                if (errCode == ResponseResult.ResultStatus.OK) {
                    if (!TextUtils.isEmpty(resultValue)) {
                        UserinfoDetailResult result = new Gson().fromJson(resultValue, UserinfoDetailResult.class);
                        if (null != result && null != result.getRes() && null != result.getRes().getUser()) {
                            if (null != result.getRes().getUser()) {
                                UserManager.ins().saveUserInfo(result.getRes().getUser());
                                refreshUi(result.getRes().getUser());
                            }
                        }
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            switch (what) {
                case EventTag.ACCOUNT_UPDATE_INFO:
                    initData();
                    break;
            }

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llAvatarContainer:
            case R.id.rlHeader:
            case R.id.tvSignature:
                SomeoneHomepageActivity.start(getContext(), UserManager.ins().getLoginUser(), false);
                break;
            case R.id.llCountInfo:
                break;
            case R.id.rlDynamic:
                SomeOneDynamicActivity.start(getActivity(), UserManager.ins().getLoginUser(), false);
                break;
            case R.id.rlMyCollect:
                MyCollectionActivity.start(getActivity());
                break;
            case R.id.rlMyFollow:
                MyFollowActivity.start(getActivity());
                break;
            case R.id.rlBrowserHistory:
                MyBrowseHistoryActivity.start(getActivity());
                break;
            case R.id.rlSetting:
                SettingActivity.start(getContext());
                break;
            case R.id.rlMyFriend:
                UserFriendActivity.start(getActivity());
                break;
            case R.id.llStory:
                SomeoneStoryActivity.start(getContext(), UserManager.ins().getLoginUser());
                break;
            case R.id.llAnswer:
                SomeoneAnswerActivity.start(getContext(), UserManager.ins().getLoginUser());
                break;
            case R.id.llDynamic:
                SomeOneDynamicActivity.start(getContext(), UserManager.ins().getLoginUser(), true);
                break;
            case R.id.llQuestion:
                SomeoneQuestionActivity.start(getContext(), UserManager.ins().getLoginUser());
                break;
            case R.id.llArticle:
                SomeoneArticleActivity.start(getContext(), UserManager.ins().getLoginUser());
                break;
            case R.id.rlHelpCenter:
                HelpCenterActivity.start(getContext());
                break;
            case R.id.rlAboutUs:
                AboutActivity.start(getContext());
                break;
            case R.id.rl_invite_redbag:
                InvateRedbagActivity.start(getContext());
                break;
            case R.id.rl_my_income:
                startActivity(new Intent(getContext(), MyIncomeActivity.class));
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView && null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventManager.ins().removeListener(EventTag.ACCOUNT_UPDATE_INFO, eventListener);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


}
